<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-button v-if="isAuth('sys:menu:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="menus"
      row-key="id"
      border
      style="width: 100%; ">

      <el-table-column
        prop="deptName"
        header-align="center"
        align="center"
        width="400"
        label="部门名称">
      </el-table-column>

      <el-table-column
        prop="deptCode"
        header-align="center"
        align="center"
        width="300"
        label="部门编码">
      </el-table-column>

      <el-table-column
        prop="parentName"
        header-align="center"
        align="center"
        width="300"
        label="上级部门">
      </el-table-column>
      <el-table-column
        prop="sort"
        header-align="center"
        align="center"
        width="300"
        label="排序号">
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        mix-width="200"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:menu:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('sys:menu:delete')" type="text" size="small" @click="deleteHandle(scope.row.id,scope.row.deptName)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './dept-add-or-update'
  export default {
    data () {
      return {
        dataForm: {},
        menus: [],
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    // 方法集合
    methods: {
      getDataList () {
        this.$http({
          url: this.$http.adornUrl('/user/dept/list/tree'),
          method: 'get'
        }).then(({data}) => {
          console.log('成功获取到菜单数据...', JSON.stringify(data.data))
          this.menus = data.data
        })
      },
      edit (data) {
        console.log('要修改的数据', JSON.stringify(data))
        this.dialogType = 'edit'
        this.title = '修改分类'
        this.dialogVisible = true

        // 发送请求获取当前节点最新的数据
        this.$http({
          url: this.$http.adornUrl(`/user/dept/info/${data.id}`),
          method: 'get'
        }).then(({data}) => {
          // 请求成功
          console.log('要回显的数据', JSON.stringify(data))
          this.dept.deptName = data.dept.deptName
          this.dept.deptCode = data.dept.deptCode
          this.dept.id = data.dept.id
          this.dept.parentId = data.dept.parentId
          this.dept.sort = data.dept.sort
        })
      },

      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },

      deleteHandle (id, name) {
        var ids = [id]
        this.$confirm(`是否删除【${name}】部门?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.$http({
              url: this.$http.adornUrl('/user/dept/delete'),
              method: 'post',
              data: this.$http.adornData(ids, false)
            }).then(({data}) => {
              if(data.code === 0){
                this.$message({
                  message: '部门删除成功',
                  type: 'success'
                })
                // 刷新出新的菜单
                this.getDataList();
              } else {
                this.$message({
                  message: data.msg,
                  type: 'error'
                })
              }
            }).catch(() => {
              this.$message({
                message: '网络出小差了,请稍后再试！',
                type: 'error'
              })
            })
          }).catch(() => {

          })
      }
    }
  }
</script>
<style scoped>
</style>
